<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag } from 'element-plus'
import { Table } from '@/components/Table'
import {getTableListApi, updsaveTableApi, delTableListApi, getMicroreviewTabList,historyTodayCount} from '@/api/table'
import { useTable } from '@/hooks/web/WQuseTable'
import { TableData } from '@/api/table/types'
import {h, ref, unref, reactive, computed} from 'vue'
import Write from './components/Write.vue'
import Detail from './components/Detail.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'

const { register, tableObject, methods } = useTable<TableData>({
  getListApi: getMicroreviewTabList,
  delListApi: delTableListApi,
  response: {
    list: 'list',
    total: 'total'
  },
  defaultParams: {
    title: ''
  }
})

const { getList, setSearchParams } = methods

getList()

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'account',
    label: t('tableDemo.account')
  },
  {
    field: 'nickname',
    label: t('tableDemo.nickname')
  },
  {
    field: 'course_name',
    label: t('tableDemo.course_wz'),
  },
  {
    field: 'comment_date',
    label: t('tableDemo.comment_date'),
  },
  {
    field: 'content',
    label: t('tableDemo.contentComment'),
  },
  {
    field: 'status',
    label: t('tableDemo.status'),
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      return h(
          ElTag,
          {
            type: cellValue === 1 ? 'success' : cellValue === 2 ? 'warning' : 'danger'
          },
          () =>
              cellValue === 1
                  ? t('tableDemo.important1')
                  : cellValue === 2
                      ? t('tableDemo.good1')
                      : t('tableDemo.commonly1')

      )
    }
  },
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action'),
    form: {
      show: false
    },
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const delLoading = ref(false)

const delData = async (row: TableData | null, multiple: boolean) => {
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.id) : tableObject.currentRow?.id as string,
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}

const actionType = ref('')

const action = (row: TableData, type: string) => {
  dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  actionType.value = type
  tableObject.currentRow = row
  tableObject.id = row.id
  dialogVisible.value = true
}

const writeRef = ref<ComponentRef<typeof Write>>()

const loading = ref(false)

const save = async () => {
  const res = await updsaveTableApi(
      {
        id: tableObject.id,
        status: 2
      }
  ).catch(() => {})
      .finally(() => {
        loading.value = false
      })
  if (res) {
    dialogVisible.value = false
    tableObject.currentPage = 1
    getList()
  }
}

const save1 = async () => {
  const res = await updsaveTableApi(
      {
        id: tableObject.id,
        status: 1
      }
  ).catch(() => {})
      .finally(() => {
        loading.value = false
      })
  if (res) {
    dialogVisible.value = false
    tableObject.currentPage = 1
    getList()
    aaa.value = 11
  }
}

const save2 = async () => {
  const res = await updsaveTableApi(
      {
        id: tableObject.id,

      }
  ).catch(() => {})
      .finally(() => {
        loading.value = false
      })
  if (res) {
    dialogVisible.value = false
    tableObject.currentPage = 1
    getList()
    aaa.value = 11
  }
}

const aaa = ref('');
  aaa.value = 11;
const getList1 = async () => {
  aaa.value = 11;
  getList();
}
const lanjie = async (row) => {
  const res = await getMicroreviewTabList(
      {
        type: 3,
        userType: 2,
        status: 2
      }
  ).catch(() => {})
      .finally(() => {
        loading.value = false
      })
  if (res) {
    tableObject.tableList = res.data
    tableObject.total = res.data.length
    aaa.value = 22;
  }
}
const dele = async () => {
  const res = await getMicroreviewTabList(
      {
        type: 2,
        userType: 2,
        status: 3
      }
  ).catch(() => {})
      .finally(() => {
        loading.value = false
      })
  if (res) {
    tableObject.tableList = res.data
    tableObject.total = res.data.length
    aaa.value = 33
  }
}
const num = ref('');
const num1 = ref('');
const num2 = ref('');
const num3 = ref('');
const getnum = async () => {
  const res = await historyTodayCount(
      {
        type: 2,
      }
  ).catch(() => {})
      .finally(() => {
        loading.value = false
      })
  if (res) {
    num.value = res.data.historyNumber
    num1.value = res.data.historyCount
    num2.value = res.data.todayNumber
    num3.value = res.data.todayCount
  }
}
getnum()
</script>

<template>
  <ContentWrap>
    <div class="mb-10px">
      <ElButton type="success" @click="getList1">{{ t('tableDemo.important1') }}</ElButton>
      <ElButton type="warning" @click="lanjie">{{ t('tableDemo.good1') }}</ElButton>
      <ElButton type="danger" @click="dele">{{ t('tableDemo.commonly1') }}</ElButton>
    </div>
    <div>
      <table id="tab">
        <tr>
          <td width="200px">历史评论人数</td>
          <td width="200px">历史评论次数</td>
          <td width="200px">今评论人数</td>
          <td width="200px">今史评论次数</td>
        </tr>
        <tr>
          <td>{{num}}</td>
          <td>{{num1}}</td>
          <td>{{num2}}</td>
          <td>{{num3}}</td>
        </tr>
      </table>
    </div>
<!--    <Table-->
<!--        :columns="crudSchemas"-->
<!--        :data="tableDataList"-->
<!--        :loading="loading"-->
<!--        :defaultSort="{ prop: 'display_time', order: 'descending' }"-->
<!--    ></Table>-->
    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="crudSchemas"
      :data="tableObject.tableList.slice((tableObject.currentPage-1)*tableObject.pageSize,tableObject.currentPage*tableObject.pageSize)"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton type="primary" v-if="aaa === 22" v-hasPermi="['example:dialog:edit']" @click="action(row, 'hf')">
          恢复
        </ElButton>
        <ElButton type="primary" v-if="aaa === 33" v-hasPermi="['example:dialog:edit']" @click="action(row, 'ql')">
          清理
        </ElButton>
        <ElButton type="primary" v-if="aaa === 11" v-hasPermi="['example:dialog:edit']" @click="action(row, 'edit')">
          {{ t('exampleDemo.edit') }}
        </ElButton>
        <ElButton type="success" v-if="aaa !== 33" v-hasPermi="['example:dialog:view']" @click="action(row, 'detail')">
          {{ t('exampleDemo.detail') }}
        </ElButton>
        <ElButton type="danger" v-if="aaa !== 33" v-hasPermi="['example:dialog:delete']" @click="delData(row)">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle" >

    <div v-if="actionType === 'edit'">是否要拦截此评论</div>
    <div v-if="actionType === 'hf'">是否要恢复此评论</div>
    <div v-if="actionType === 'ql'">是否要彻底删除此评论</div>
    <Detail
      v-if="actionType === 'detail'"
      :detail-schema="allSchemas.detailSchema"
      :current-row="tableObject.currentRow"
    />

    <template #footer>
      <ElButton v-if="actionType === 'edit'" type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton v-if="actionType === 'hf'" type="primary" :loading="loading" @click="save1">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton v-if="actionType === 'ql'" type="primary" :loading="loading" @click="save2">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
    </template>
  </Dialog>
</template>
<style scoped>
#tab{
  border-color: black;
}
</style>